import { reactive, ref } from "vue"
import { useDict } from "@/dict"
export function useFormula(useCommonState){
	const { useDictList } = useDict()
	const useState = reactive({
		category: useDictList('formulaType'),
		modeCategory: useDictList('formulaModule'),
		// category: [
		//   { type: 1, label: '加法', addition: true },
		//   { type: 2, label: '减法', subtraction: true },
		//   { type: 3, label: '混合', blend: true },
		// ],
		// modeCategory: [
		//   { type: 1, label: "结果", result: true },
		//   { type: 2, label: "填空", blanks: true },
		//   { type: 3, label: "混合", blend: true },
		// ],
		// rangeValue: [0, 10], // 开始值，结束值
		// // 开始值
		// startValue: 0,
		// // 结束值
		// endValue: 10,
		// // 算式数量 
		// topicNumber: 0,
		// // 列表
		// topicList: [],
		// // current
		// current: null,
		// // current: { type: 1, label: '加法', addition: true },
		// // mode
		// mode: null,
		// // mode: { type: 2, label: "填空", blanks: true },
		// // 整10、整5
		// resultValue: 0
	})
  // 随机数
  const getRandomNumber = (min, max) => {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }
  // 是否偶数
  const isZreo = () => {
    const number = getRandomNumber(0, 100)
    return (number % 2) === 0
  }
  // 加法算式模式 - 结果
  const formulaAddResult = () => {
    const start = getRandomNumber(useCommonState.rangeValue[0], useCommonState.rangeValue[1])
    const end = getRandomNumber(0, useCommonState.rangeValue[1] - start)
	
	// const formula = [start, '+', end, '=', null]
    const formula = {
		formula: [start, '+', end, '=', null],
		result: start + end,
		input: '',
		validate: null,
		merge: true
	};
    useCommonState.topicList.push(formula)
  }
  // 减法算式模式 - 结果
  const formulaSubResult = () => {
    const start = getRandomNumber(useCommonState.rangeValue[0], useCommonState.rangeValue[1])
    const end = getRandomNumber(0, start)
	
    // const formula = [start, '-', end, '=', null]
	const formula = {
		formula: [start, '-', end, '=', null],
		result: start - end,
		input: '',
		validate: null,
		merge: true
	};
    useCommonState.topicList.push(formula)
  }

  // 加法算式模式 - 填空
  const formulaAddBlanks = () => {
    // 空格位置
    let spaceIdx = isZreo() ? 0 : 1
    // 结果
    const end = useCommonState.resultValue || getRandomNumber(useCommonState.rangeValue[0], useCommonState.rangeValue[1])
    // 可加的数字
    const start = getRandomNumber(0, end)
    // 算式集合
    const formula = [];
    formula[0] = spaceIdx === 0 ? null : start
    formula[1] = '+'
    formula[2] = spaceIdx === 1 ? null : start
    formula[3] = '='
    formula[4] = end
	const f = {
		formula,
		result: end - start,
		input: '',
		validate: null,
		split: true
	}
    useCommonState.topicList.push(f)
  }

  // 减法算式模式 - 填空
  const formulaSubBlanks = () => {
    // 空格位置
    let spaceIdx = isZreo() ? 0 : 1
    // 结果
    const end = getRandomNumber(useCommonState.rangeValue[0], useCommonState.rangeValue[1])
    // 可加的数字
    const start = spaceIdx === 0 ? getRandomNumber(0, useCommonState.rangeValue[1] - end) : getRandomNumber(end, useCommonState.rangeValue[1])
    // 可加的数字
    // 算式集合
    const formula = [];
    formula[0] = spaceIdx === 0 ? null : start
    formula[1] = '-'
    formula[2] = spaceIdx === 1 ? null : start
    formula[3] = '='
    formula[4] = end
	const f = {
		formula,
		result: spaceIdx === 0 ? end + start : start - end,
		input: '',
		validate: null,
		split: true
	}
    useCommonState.topicList.push(f)
  }


  /**
   * 加法算式
   */
  const useAdditionFormula = () => {
    if(useCommonState.mode.result) { formulaAddResult() }
    if(useCommonState.mode.blanks) { formulaAddBlanks() }
    if(useCommonState.mode.blend) {
      const idx = getRandomNumber(0, 1)
      idx === 0 ? formulaAddResult() : formulaAddBlanks()
    }
  }

  /**
   * 减法算式
   */
  const useSubtractionFormula = () => {
    if(useCommonState.mode.result) { formulaSubResult() }
    if(useCommonState.mode.blanks) { formulaSubBlanks() }
    if(useCommonState.mode.blend) {
      const idx = getRandomNumber(0, 1)
      idx === 0 ? formulaSubResult() : formulaSubBlanks()
    }
  }

  /**
   * 混合算式
   */
  const useBlendFormula = () => {
      isZreo() ? useAdditionFormula() : useSubtractionFormula()
  }

  /**
   * 生成算式
   */
  const useCreateFormula = () => {
	  return new Promise((resolve, reject) => {
		  if(!useCommonState.topicNumber) {
			  wx.showToast({title:"请选择数量", icon: "none"})
			  reject()
			  return
		  }
		  if(!useCommonState.current) {
			  wx.showToast({title:"请选择算式类型", icon: "none"})
			  reject()
			  return
		  }
		  if(!useCommonState.mode) {
			  wx.showToast({title:"请选择算式模式", icon: "none"})
			  reject()
			  return
		  }
		  useCommonState.topicList = []
		if(useCommonState.current.addition) { 
		  for(let i = 0; i < useCommonState.topicNumber; i++){
			useAdditionFormula()
		  }
		}
		if(useCommonState.current.subtraction) { 
		  for(let i = 0; i < useCommonState.topicNumber; i++){
			useSubtractionFormula()
		  }
		}
		if(useCommonState.current.blend) { 
		  for(let i = 0; i < useCommonState.topicNumber; i++){
			useBlendFormula()
		  }
		}
		resolve()
	})
  }


  
  return {
    useState,
	useCommonState,
    useCreateFormula,
  }
}